package com.ask.bitmap;

public class BitMap {
    int size; // 数字范围是0~size - 1
    int[] bitMap;

    public BitMap(int size) {
        this.size = size;
        this.bitMap = new int[size / 32 + 1];
    }

    public void setBit(int num) {
        if (num >= size) return;
        int index = num / 32;
        int bit = num % 32;
        bitMap[index] |= (1 << bit); // 将bitMap[index]的bit位置1
    }

    public boolean isSet(int num) {
        if (num >= size) return false;
        int index = num / 32;
        int bit = num % 32;
        return (bitMap[index] & (1 << bit)) != 0;
    }

    void clearBit(int num) {
        if (num >= size) return;
        int index = num / 32;
        int bit = num % 32;
        bitMap[index] &= ~(1 << bit);
    }
}
